<template>
	<view class="content">
		<view class="search-box">
			<view class="img" @click="returnBtn"><image src="../../static/image/follow/icon_jinru2@3x.png" mode=""></image></view>
			<!-- mSearch组件 如果使用原样式，删除组件元素-->
			<mSearch
				class="mSearch-input-box"
				:mode="2"
				button="inside"
				:placeholder="defaultKeyword"
				@search="doSearch(false)"
				@input="inputChange"
				@confirm="doSearch(false)"
				v-model="keyword"
			></mSearch>
			<!-- 原样式 如果使用原样式，恢复下方注销代码 -->
			<!-- 						
			<view class="input-box">
				<input type="text" :adjust-position="true" :placeholder="defaultKeyword" @input="inputChange" v-model="keyword" @confirm="doSearch(false)"
				 placeholder-class="placeholder-class" confirm-type="search">
			</view>
			<view class="search-btn" @tap="doSearch(false)">搜索</view> 
			 -->
			<!-- 原样式 end -->
		</view>
		<view class="search-keyword">
			<scroll-view class="keyword-list-box" v-show="isShowKeywordList" scroll-y>
				<!-- 	<block v-for="(row, index) in keywordList" :key="index">
					<view class="keyword-entry" hover-class="keyword-entry-tap">
						<view class="keyword-text" @tap.stop="doSearch(keywordList[index].keyword)"><rich-text :nodes="row.htmlStr"></rich-text></view>
						<view class="keyword-img" @tap.stop="setKeyword(keywordList[index].keyword)"><image src="/static/HM-search/back.png"></image></view>
					</view>
				</block> -->
				<recommend v-for="(item, index) in keywordList" :key="index" :param="item" @shopDetail="shopDetail" @bayNow="bayNow" @share="share"></recommend>
			</scroll-view>
			<scroll-view class="keyword-box" v-show="!isShowKeywordList" scroll-y>
				<view class="keyword-block" v-if="oldKeywordList.length > 0">
					<view class="keyword-list-header">
						<view>历史搜索</view>
						<view><image @tap="oldDelete" src="../../static/image/HM-search/delete.png"></image></view>
					</view>
					<view class="keyword">
						<view v-for="(keyword, index) in oldKeywordList" @tap="doSearch(keyword)" :key="index">{{ keyword }}</view>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="motal" v-show="motalShow" :class="{ rgba: listShow }">
			<view class="screen">
				<view class="content">
					<view class="all">
						<text class="text" @click="isShow(0)">全部类型</text>
						<image src="../../static/image/index/XLXZ@3x.png" mode=""></image>
					</view>
					<view class="near" @click="isShow(1)">
						<text>附近</text>
						<image src="../../static/image/index/XLWXZ@3x.png" mode=""></image>
					</view>
					<view class="price" @click="price(1)" v-if="sort == 1">
						<text>价格</text>
						<image src="../../static/image/index/XLWXZ@3x.png" mode=""></image>
					</view>
					<view class="price" @click="price(2)" v-if="sort == 2">
						<text>价格</text>
						<image src="../../static/image/search/XLXZ1@3x.png" mode=""></image>
					</view>
					<view class="price" @click="price(3)" v-if="sort == 3">
						<text>价格</text>
						<image src="../../static/image/index/XLXZ@3x.png" mode=""></image>
					</view>
					<view class="sale" @click="xiaoliangs(1)" v-if="xiaoliang == 1">
						<text>销量</text>
						<image src="../../static/image/index/XLWXZ@3x.png" mode=""></image>
					</view>
					<view class="sale" @click="xiaoliangs(2)" v-if="xiaoliang == 2">
						<text>销量</text>
						<image src="../../static/image/search/XLXZ1@3x.png" mode=""></image>
					</view>
					<view class="sale" @click="xiaoliangs(3)" v-if="xiaoliang == 3">
						<text>销量</text>
						<image src="../../static/image/index/XLXZ@3x.png" mode=""></image>
					</view>
				</view>
				<!-- 类型 -->
				<view class="list" v-if="selectList == 0">
					<view class="leftItem">
						<view class="text" v-for="(item, index) in screenList" @click="bind_id(item.id)" v-if="item.pid == 0" :class="{ activLeftItem: item.id == id }">
							{{ item.title }}
						</view>
					</view>
					<view class="rightItem">
						<view class="text" v-for="(item, index) in screenList" @click="bind_id_id(item.id)" v-if="item.pid == id" :class="{ activLeftItem: item.id == id_id }">
							{{ item.title }}
						</view>
					</view>
				</view>
				<!-- 附近 -->
				<view class="list" v-if="selectList == 1">
					<view class="leftItem">
						<view class="text" @click="bindArea('附近')" :class="{ activLeftItem: fujin == '附近' }">附近</view>
						<view class="text" @click="bindArea('龙泉驿区')" :class="{ activLeftItem: fujin == '龙泉驿区' }">龙泉驿区</view>
					</view>
					<view class="rightItem">
						<view class="text" @click="bindNear(500)" :class="{ activLeftItem: fujin_juli == '500' }">附近500米</view>
						<view class="text" @click="bindNear(1000)" :class="{ activLeftItem: fujin_juli == '1000' }">附近1公里</view>
						<view class="text" @click="bindNear(2000)" :class="{ activLeftItem: fujin_juli == '2000' }">附近2公里</view>
						<view class="text" @click="bindNear(5000)" :class="{ activLeftItem: fujin_juli == '5000' }">附近5公里</view>
					</view>
				</view>
				<!-- 按钮 -->
				<view class="btn" v-show="btnShow">
					<view class="resetBtn">重置</view>
					<view @click="sureBtn" class="sureBtn">确定</view>
				</view>
			</view>
		</view>
		<view class="quesheng" v-show="queshengShow">
			<view class="img"><image src="../../static/image/follow/ZWGZ@3x.png" mode=""></image></view>
			<view class="text1">暂无内容</view>
			<view class="text2">试试搜索其他商品吧~</view>
		</view>
	</view>
</template>

<script>
//引用mSearch组件，如不需要删除即可
import mSearch from '@/components/mehaotian-search-revision/mehaotian-search-revision.vue'; //加载搜索组件
import recommend from '../../components/recommend.vue'; //列表组件
import screen from '../../components/screen.vue'; //筛选列表
export default {
	data() {
		return {
			defaultKeyword: '',
			keyword: '',
			oldKeywordList: [],
			hotKeywordList: [],
			keywordList: [],
			forbid: '',
			isShowKeywordList: false, //列表框展示
			motalShow: false, //筛选框
			screenList: '', //筛选数据,
			id: '1', //一级菜单样式参数
			id_id: '', //二菜单样式参数
			selectList: '-1', //下拉列表
			listShow: '', //下拉蒙版设置
			fujin: '附近', //附近列表样式设置
			fujin_juli: '500', //附近列表二级菜单
			dataPost: {
				store_area: '龙泉驿区',
				lat: '',
				lng: '',
				first_cate: 0,
				second_cate: 0,
				distance: 0,
				price: 0,
				volume: 0,
				title: 0
			},
			sort: '1', //价格排序 1升2 降
			xiaoliang: '1', //销量排序
			queshengShow: false, //缺省展示隐藏
			btnShow: false //按钮显示隐藏
		};
	},
	onLoad() {
		this.init();
		this.ajaxJson('getClassify', {}, res => {
			this.screenList = res;
		});
		this.ajaxJson('keywordLog', { keyword: '' }, res => {
			this.oldKeywordList = res;
		});
	},
	components: {
		//引用mSearch组件，如不需要删除即可
		mSearch,
		recommend, //列表组件
		screen //筛选组件
	},
	methods: {
		share(e){
			console.log(e)
			uni.navigateTo({
				url:'/pages/share/poster?goods_id='+e.id+"&bid="+e.bid+"&img_url=" + e.img_url
			})
		},
		init() {
			this.loadDefaultKeyword();
			this.loadOldKeyword();
		},

		blur() {
			uni.hideKeyboard();
		},
		//加载默认搜索关键字
		loadDefaultKeyword() {
			//定义默认搜索关键字，可以自己实现ajax请求数据再赋值,用户未输入时，以水印方式显示在输入框，直接不输入内容搜索会搜索默认关键字
			this.defaultKeyword = '默认关键字';
		},
		//加载历史搜索,自动读取本地Storage
		loadOldKeyword() {
			// if (uni.getStorageSync('OldKeys')) {
			// 	uni.getStorage({
			// 		key: 'OldKeys',
			// 		success: res => {
			// 			var OldKeys = JSON.parse(res.data);
			// 			this.oldKeywordList = OldKeys;
			// 		}
			// 	});
			// }
			this.ajaxJson('keywordLog', { keyword: '' }, res => {
				this.oldKeywordList = res;
			});
		},
		//监听输入
		inputChange(event) {
			//兼容引入组件时传入参数情况
			var keyword = event.detail ? event.detail.value : event;
			if (!keyword) {
				this.keywordList = [];
				this.isShowKeywordList = false;
				this.motalShow = false;
				return;
			}
			this.isShowKeywordList = true;
			// //以下示例截取淘宝的关键字，请替换成你的接口
			// uni.request({
			// 	url: 'https://suggest.taobao.com/sug?code=utf-8&q=' + keyword, //仅为示例
			// 	success: res => {
			// 		this.keywordList = [];
			// 		this.keywordList = this.drawCorrelativeKeyword(res.data.result, keyword);
			// 	}
			// });
		},
		//高亮关键字
		drawCorrelativeKeyword(keywords, keyword) {
			var len = keywords.length,
				keywordArr = [];
			for (var i = 0; i < len; i++) {
				var row = keywords[i];
				//定义高亮#9f9f9f
				var html = row[0].replace(keyword, "<span style='color: #9f9f9f;'>" + keyword + '</span>');
				html = '<div>' + html + '</div>';
				var tmpObj = {
					keyword: row[0],
					htmlStr: html
				};
				keywordArr.push(tmpObj);
			}
			return keywordArr;
		},
		//顶置关键字
		setKeyword(index) {
			this.keyword = this.keywordList[index].keyword;
		},
		//清除历史搜索
		oldDelete() {
			uni.showModal({
				content: '确定清除历史搜索记录？',
				success: res => {
					if (res.confirm) {
						console.log('用户点击确定');
						this.ajaxJson('delkeywordLog', {}, res => {
							this.oldKeywordList = [];
						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		//热门搜索开关
		// hotToggle() {
		// 	this.forbid = this.forbid ? '' : '_forbid';
		// },
		//执行搜索
		doSearch(keyword) {
			this.keyword = keyword ? keyword : this.keyword;
			// this.saveKeyword(keyword); //保存为历史
			this.isShowKeywordList = true;
			this.motalShow = true; //显示筛选
			this.ajaxJson('keywordLog', { keyword: this.keyword }, res => {});
			// let dataPost = {
			// 	store_area: '龙泉驿区',
			// 	lat: '',
			// 	lng: '',
			// 	first_cate: 0,
			// 	second_cate: 0,
			// 	distance: 40,
			// 	price: 1,
			// 	volume: 0,
			// 	title: 31
			// };
			console.log(this.dataPost);
			if (this.keyword) {
				this.dataPost.first_cate = 0;
				this.dataPost.second_cate = 0;
				this.dataPost.title = this.keyword;
				this.dataPost.distance = 0;
				this.dataPost.price = 0;
				this.dataPost.volume = 0;
			} else {
				this.dataPost.title = 0;
			}

			this.ajaxJson('hots', this.dataPost, ret => {
				if (ret.length == 0) {
					this.queshengShow = true;
				} else {
					this.keywordList = ret;
					this.queshengShow = false;
				}
			});
		},
		//保存关键字到历史记录
		saveKeyword(keyword) {
			uni.getStorage({
				key: 'OldKeys',
				success: res => {
					var OldKeys = JSON.parse(res.data);
					var findIndex = OldKeys.indexOf(keyword);
					if (findIndex == -1) {
						OldKeys.unshift(keyword);
					} else {
						OldKeys.splice(findIndex, 1);
						OldKeys.unshift(keyword);
					}
					//最多10个纪录
					OldKeys.length > 10 && OldKeys.pop();
					uni.setStorage({
						key: 'OldKeys',
						data: JSON.stringify(OldKeys)
					});
					this.oldKeywordList = OldKeys; //更新历史搜索
				},
				fail: e => {
					var OldKeys = [keyword];
					uni.setStorage({
						key: 'OldKeys',
						data: JSON.stringify(OldKeys)
					});
					this.oldKeywordList = OldKeys; //更新历史搜索
				}
			});
		},
		//返回上级
		returnBtn: function() {
			uni.reLaunch({
				url:'../index/index'
			});
		},
		//显示隐藏列表
		isShow: function(e) {
			this.selectList = e;
			this.listShow = true;
			this.btnShow = true;
		},
		//关闭组件
		resetBtn: function() {
			this.$emit('setNavBar');
		},
		//全部列表一级菜单样式改变
		bind_id: function(id) {
			this.id = id;
			this.dataPost.first_cate = id;
		},
		//全部列表二级菜单样式改变
		bind_id_id: function(id) {
			this.id_id = id;
			this.dataPost.second_cate = id;
		},
		//附近一级列表
		bindArea: function(e) {
			this.dataPost.store_area = e;
			this.fujin = e;
		},
		//附近二级列表
		bindNear: function(e) {
			this.dataPost.distance = e;
			this.fujin_juli = e;
		},
		//价格排序
		price: function(e) {
			this.dataPost.price = e;
			this.sort = e == 3 ? 1 : e+1;
		},
		//销量排序
		xiaoliangs: function(e) {
			this.dataPost.volume = e;
			this.xiaoliang = e == 3 ? 1 : e+1;
		},
		//确定按钮
		sureBtn: function() {
			this.doSearch;
			this.selectList = -1;
			this.btnShow = false;
			this.listShow = false;
		},
		//进入详情
		shopDetail: function(item) {
			// console.log(item);
			// return false;
			this.ajaxJson('selStock', { goods_id: item.id }, res => {
				console.log(res);
				if (res > 0) {
					
					uni.navigateTo({
						url: '../shopDetails/shopDetails?goods_id=' + item.id + '&bid=' + item.bid + '&first_cate=' + item.first_cate + '&secend_cate=' + item.secend_cate
					});
				} else {
					console.log(55);
					uni.showToast({
						title: '抱歉，该商品已售罄'
					});
					this.doSearch();
				}
			});
		},
		// 价格销量图片切换
		imageChange:function(index){
			
		}
	}
};
</script>
<style lang="scss">
view {
	display: block;
}
* {
	padding: 0;
	margin: 0;
	font-size: 0.75rem;
	box-sizing: border-box;
}
.search-box {
	width: 95%;
	background-color: rgb(242, 242, 242);
	padding: 15upx 2.5%;
	display: flex;
	justify-content: space-between;
	align-items: center;
	position: sticky;
	top: 0;
}
.search-box .mSearch-input-box {
	width: 100%;
}
.search-box .input-box {
	width: 85%;
	flex-shrink: 1;
	display: flex;
	justify-content: center;
	align-items: center;
}
.search-box .search-btn {
	width: 15%;
	margin: 0 0 0 2%;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-shrink: 0;
	font-size: 28upx;
	color: #fff;
	background: linear-gradient(to right, #ff9801, #ff570a);
	border-radius: 60upx;
}
.search-box .input-box > input {
	width: 100%;
	height: 60upx;
	font-size: 32upx;
	border: 0;
	border-radius: 60upx;
	-webkit-appearance: none;
	-moz-appearance: none;
	appearance: none;
	padding: 0 3%;
	margin: 0;
	background-color: #ffffff;
}
.placeholder-class {
	color: #9e9e9e;
}
.search-keyword {
	width: 100%;
	background-color: rgb(242, 242, 242);
}
.keyword-list-box {
	height: calc(100vh - 110upx);
	padding-top: 10upx;
	border-radius: 20upx 20upx 0 0;
	background-color: #fff;
}
.keyword-entry-tap {
	background-color: #eee;
}
.keyword-entry {
	width: 94%;
	height: 80upx;
	margin: 0 3%;
	font-size: 30upx;
	color: #333;
	display: flex;
	justify-content: space-between;
	align-items: center;
	border-bottom: solid 1upx #e7e7e7;
}
.keyword-entry image {
	width: 60upx;
	height: 60upx;
}
.keyword-entry .keyword-text,
.keyword-entry .keyword-img {
	height: 80upx;
	display: flex;
	align-items: center;
}
.keyword-entry .keyword-text {
	width: 90%;
}
.keyword-entry .keyword-img {
	width: 10%;
	justify-content: center;
}
.keyword-box {
	height: calc(100vh - 110upx);
	border-radius: 20upx 20upx 0 0;
	background-color: #fff;
}
.keyword-box .keyword-block {
	padding: 10upx 0;
}
.keyword-box .keyword-block .keyword-list-header {
	width: 94%;
	padding: 10upx 3%;
	font-size: 27upx;
	color: #333;
	display: flex;
	justify-content: space-between;
}
.keyword-box .keyword-block .keyword-list-header image {
	width: 40upx;
	height: 40upx;
}
.keyword-box .keyword-block .keyword {
	width: 94%;
	padding: 3px 3%;
	display: flex;
	flex-flow: wrap;
	justify-content: flex-start;
}
.keyword-box .keyword-block .hide-hot-tis {
	display: flex;
	justify-content: center;
	font-size: 28upx;
	color: #6b6b6b;
}
.keyword-box .keyword-block .keyword > view {
	display: flex;
	justify-content: center;
	align-items: center;
	border-radius: 60upx;
	padding: 0 20upx;
	margin: 10upx 20upx 10upx 0;
	height: 60upx;
	font-size: 28upx;
	background-color: rgb(242, 242, 242);
	color: #6b6b6b;
}
.img {
	margin-right: 20rpx;
	width: 40rpx;
	height: 40rpx;
	/* margin-top: 20rpx; */
}
.img image {
	width: 100%;
	height: 100%;
}
.motal {
	width: 100%;
	position: fixed;
	top: 80rpx;
	left: 0;
	right: 0;
	// border: 1px red solid;
}
.screen {
	display: flex;
	flex-direction: column;
	align-items: center;
	.content {
		width: 100%;
		background: #ffffff;
		display: flex;
		justify-content: space-around;
		padding: 10rpx;
		text-align: center;
		image {
			width: 20rpx;
			height: 20rpx;
			margin-left: 7rpx;
		}
	}
	.list {
		width: 100%;
		background-color: #f4f4fa;
		display: flex;
		.leftItem {
			width: 40%;
			text-align: center;
			.text {
				background-color: #ffffff;
				padding: 20rpx 40rpx;
			}
		}
		.activLeftItem {
			background-color: #f4f4fa !important;
			color: #fd6431;
		}
		.rightItem {
			width: 60%;
			text-align: center;
			.text {
				padding: 20rpx 40rpx;
			}
		}
	}
	.btn {
		width: 100%;
		display: flex;
		justify-content: space-around;
		text-align: center;
		background-color: #ffffff;
		border-top: 1px #f4f4fa solid;
		padding: 20rpx 40rpx;
		.resetBtn,
		.sureBtn {
			width: 40%;
			padding: 15rpx 0;
			border-radius: 50rpx;
		}
		.resetBtn {
			border: 1px #fd6431 solid;
			color: #fd6431;
		}
		.sureBtn {
			background: linear-gradient(to right, #fd6431, #fea789);
			color: #ffffff;
		}
	}
}
.rgba {
	height: 100%;
	background-color: rgba(0, 0, 0, 0.5);
}
//缺省页
.quesheng {
	position: fixed;
	top: 400rpx;
	bottom: 0;
	left: 0;
	right: 0;
	width: 100%;
	height: 100%;
	// border: 1px red solid;
	display: flex;
	flex-direction: column;
	align-items: center;
	.img {
		margin: 0 auto;
		width: 300rpx;
		height: 300rpx;
		image {
			width: 100%;
			height: 100%;
		}
	}
	.text1 {
		margin-top: 10rpx;
		color: #4e5366;
	}
	.text2 {
		margin-top: 20rpx;
		font-size: 32rpx;
		font-weight: bold;
	}
}
</style>
